package com.genexususa.eo.bing.data.results;

import com.genexususa.eo.bing.CampaignManagementServiceClasses.*;
import com.genexususa.eo.bing.data.ErrorWSCollection;
import com.genexususa.eo.bing.data.GxException;
import com.genexususa.eo.bing.data.GxItemException;
import org.xml.sax.SAXException;

import javax.xml.bind.annotation.*;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.rpc.ServiceException;
import java.io.IOException;
import java.rmi.RemoteException;

@XmlTransient
public abstract class GxGeneralResults {
    @XmlElement(name = "TrackingId")
    public String trackingId;
    @XmlElement(name = "HasError")
    public boolean hasError;
    @XmlElement(name = "ErrorCollection")
    public ErrorWSCollection errorCollection;

    public GxGeneralResults() {
        this.hasError = false;
        this.errorCollection = new ErrorWSCollection();
    }

    public GxGeneralResults(String trackingId) {
        this.trackingId = trackingId;
        this.hasError = false;
        this.errorCollection = new ErrorWSCollection();
    }

    public void addServiceExceptionError(ServiceException e) {
        this.hasError = true;
        this.errorCollection.addServiceExceptionError(e);
    }

    public  void addIllegalArgumentExceptionError(IllegalArgumentException e) {
        this.hasError = true;
        this.errorCollection.addIllegalArgumentExceptionError(e);
    }

    public void addGxExceptionError(GxException e) {
        this.hasError = true;
        GxItemException[] errors = e.getExceptions();
        for (int i=0; i<errors.length; i++) {
            if (errors[i] != null) {
                this.addGxExceptionError(errors[i], i);
            }
        }
    }

    public void addParserConfigurationExceptionError(ParserConfigurationException e) {
        this.hasError = true;
        this.errorCollection.addParserConfigurationExceptionError(e);
    }

    public void addSAXExceptionError(SAXException e) {
        this.hasError = true;
        this.errorCollection.addSAXExceptionError(e);
    }

    public void addApiFaultDetailError(ApiFaultDetail fault) {
        this.hasError = true;
        BatchError[] errors = fault.getBatchErrors();
        for (int i=0; i<errors.length; i++) {
            this.addBatchError(errors[i]);
        }
        this.errorCollection.addOperationErrors(fault.getOperationErrors());
        this.errorCollection.addApplicationFaultError(fault);
    }

    public void addEditorialApiFaultDetailError(EditorialApiFaultDetail fault) {
        this.hasError = true;
        BatchError[] batchErrors = fault.getBatchErrors();
        for (int i=0; i<batchErrors.length; i++) {
            this.addBatchError(batchErrors[i]);
        }
        EditorialError[] editorialErrors = fault.getEditorialErrors();
        for (int i=0; i<editorialErrors.length; i++) {
            this.addEditorialError(editorialErrors[i]);
        }
        this.errorCollection.addOperationErrors(fault.getOperationErrors());
        this.errorCollection.addApplicationFaultError(fault);
    }

    public void addAdApiFaultDetailError(AdApiFaultDetail fault) {
        this.hasError = true;
        this.errorCollection.addAdApiFaultDetailError(fault);
        this.errorCollection.addApplicationFaultError(fault);
    }

    public void addRemoteExceptionError(RemoteException e) {
        this.hasError = true;
        this.errorCollection.addRemoteExceptionError(e);
    }

    public void addIOExceptionError(IOException e) {
        this.hasError = true;
        this.errorCollection.addIOExceptionError(e);
    }

    public void addGxItemExceptionError(GxItemException e) {
        this.hasError = true;
        this.errorCollection.addGxItemExceptionError(e);
    }

    public abstract void addGxExceptionError(GxItemException error, int index);
    public abstract void addBatchError(BatchError error);
    public abstract void addEditorialError(EditorialError error);
}
